//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.sql;

import com.intellij.lang.Language;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.PatternCondition;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.ProcessingContext;
import com.planw.beetl.utils.BeetlSqlConst;
import com.planw.beetl.utils.ConstUtil;
import com.planw.beetl.utils.MavenUtil;
import com.planw.beetl.utils.StrUtil;
import org.intellij.plugins.markdown.lang.MarkdownLanguage;
import org.intellij.plugins.markdown.lang.psi.impl.MarkdownCodeBlock;
import org.intellij.plugins.markdown.lang.psi.impl.MarkdownCodeFence;
import org.intellij.plugins.markdown.lang.psi.impl.MarkdownParagraph;
import org.jetbrains.annotations.NotNull;
public class MarkdownBeetlSqlPatternCondition extends PatternCondition<PsiElement> {
    private static final Logger logger = Logger.getInstance(MarkdownBeetlSqlPatternCondition.class);

    public MarkdownBeetlSqlPatternCondition() {
        super(" BeetlSqlPatternCondition ");
    }

    @Override
    public boolean accepts(@NotNull PsiElement psiElement, ProcessingContext processingContext) {
        if (psiElement == null) {
            return false;
        }

        if (!ConstUtil.isReady()) {
            logger.info("项目未准备完成");
            return false;
        } else {
            Project project = psiElement.getProject();
            Module module = ModuleUtil.findModuleForFile(psiElement.getContainingFile().getOriginalFile().getVirtualFile(), project);
            if (!MavenUtil.isDependOnBeetlSql(module)) {
                logger.info(project.getName() + "项目无beetlsql依赖库，无需代码提示");
                return false;
            } else {
                PsiFile psiFile = psiElement.getContainingFile();
                String content = psiElement.getText();
                BeetlSqlConst beetlSqlConst = ConstUtil.getBeetlSqlConst(psiFile);
                boolean isContainBeetl = StrUtil.containAny(content, new String[]{beetlSqlConst.DELIMITER_STATEMENT_START, beetlSqlConst.DELIMITER_PLACEHOLDER_START, beetlSqlConst.DELIMITER_PLACEHOLDER_START2});
                if (!isContainBeetl) {
                    logger.info("未包括BeetlSql的定界符和占位符");
                    return false;
                } else {
                    logger.info("判断内容[" + content + "]是否需要代码完成");
                    InjectedLanguageManagerImpl ilm = InjectedLanguageManagerImpl.getInstanceImpl(project);
                    boolean injectedFragment = ilm.isInjectedFragment(psiFile);
                    Language fileLanguage;
                    PsiElement parent;
                    if (injectedFragment) {
                        logger.info("属于内嵌语言");
                        PsiFile topLevelFile = ilm.getTopLevelFile(psiElement);
                        fileLanguage = topLevelFile.getOriginalFile().getLanguage();
                        PsiLanguageInjectionHost injectionHost = ilm.getInjectionHost(psiElement);
                        parent = injectionHost != null ? injectionHost.getOriginalElement() : null;
                    } else {
                        logger.info("属于非内嵌语言");
                        fileLanguage = PsiUtilCore.findLanguageFromElement(psiElement);
                        parent = psiElement.getParent();
                    }

                    boolean flag = fileLanguage.is(MarkdownLanguage.INSTANCE) && (parent instanceof MarkdownParagraph || parent instanceof MarkdownCodeFence || parent instanceof MarkdownCodeBlock);
                    logger.info("判断结果：" + flag);
                    return flag;
                }
            }
        }
    }
}
